package com.savvy.activity;

import com.savvy.R;
import com.savvy.util.TimerWithHandler;
import com.savvy.util.TimerWithHandlerFactory;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

/**
 * This is the splash screen activity.
 * 
 * @author Bennis
 * 
 */
public class SavvyActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		/**
		 * Handle timer message.
		 */
		mainHandler = new Handler() {

			public void handleMessage(Message msg) {
				switch (msg.what) {
				case TimerWithHandler.PRE_TIMER:
					Log.v(this.getClass().getName(), "pre timer");
					break;
				case TimerWithHandler.TIMEOUT:
					if (nSlide < slide.length) {
						imageView.setImageResource(slide[nSlide++]);
						imageView.invalidate();
					} else {
						TimerWithHandlerFactory.kill(splashTimer);
					}
					Log.v(this.getClass().getName(), "timer");
					break;
				case TimerWithHandler.PAUSED:
					Log.v(this.getClass().getName(), "timer paused");
					break;
				case TimerWithHandler.RESUME:
					Log.v(this.getClass().getName(), "timer resume");
					break;
				case TimerWithHandler.STOPPED:
					Log.v(this.getClass().getName(), "timer stopped");
					break;
				default:
					break;
				}
			}
		};

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);

		switch (launchType) {
		case LTYPEPIC:
			imageView = (ImageView) findViewById(R.id.layout_splash_imageview);
			imageView.setVisibility(View.VISIBLE);
			splashTimer = TimerWithHandlerFactory.create(mainHandler, period,
					TimerWithHandler.ATTR_REPEAT);
			nSlide = 0;
			TimerWithHandlerFactory.start(splashTimer);
			pause = false;

			AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
			PendingIntent pi;
			pi = PendingIntent.getService(this, 0, new Intent(this,
					SavvyActivity.class), 0);
			am.setRepeating(AlarmManager.ELAPSED_REALTIME,
					SystemClock.elapsedRealtime(), 2000, pi);
			break;

		case LTYPEVIDEO:
			break;

		case LTYPEMUSIC:
			break;

		case LTYPEPIC_MUSIC:
			break;

		case LTYPEVIDEO_MUSIC:
		default:
			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.v(this.getClass().toString(), "Key code: " + keyCode + "; Event: "
				+ event.getAction());
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			try {
				if (pause) {
					pause = false;
					TimerWithHandlerFactory.resume(splashTimer);
				} else {
					pause = true;
					TimerWithHandlerFactory.pause(splashTimer);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case MotionEvent.ACTION_UP:
			break;
		case MotionEvent.ACTION_MOVE:
			break;
		default:
			break;
		}
		Log.v(this.getClass().toString(), "Touch: " + event.getAction()
				+ "; x: " + event.getX() + "; y: " + event.getY());
		return super.onTouchEvent(event);
	}

	private static final int LTYPEPIC = 0;
	private static final int LTYPEVIDEO = 1;
	private static final int LTYPEMUSIC = 2;
	private static final int LTYPEPIC_MUSIC = 3;
	private static final int LTYPEVIDEO_MUSIC = 4;

	private long period = 5000;

	private int launchType;
	private Handler mainHandler;

	private ImageView imageView;
	private int splashTimer;
	private static final int[] slide = { R.drawable.flower, R.drawable.donga,
			R.drawable.mountainvillage };
	private int nSlide;
	private boolean pause;

}
